package com.livepass.android.girlspicks.adapters;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.livepass.android.girlspicks.R;
import com.livepass.android.girlspicks.models.Article;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class ArticleAdapter extends ArrayAdapter<Article> {

	private int resourceId;
	int num = 1;

	DisplayImageOptions options;
	ImageLoader imgLoader;

	public ArticleAdapter(Context context, int resource, List<Article> objects) {
		super(context, resource, objects);
		this.resourceId = resource;

		imgLoader = ImageLoader.getInstance();
		imgLoader.init(ImageLoaderConfiguration.createDefault(context));

		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_default)
				// .showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_default)
				.cacheInMemory(true).cacheOnDisk(true).build();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		Article article = getItem(position);
		View row = convertView;
		if (row == null) {
			LayoutInflater flater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = flater.inflate(this.resourceId, null);
		}

		TextView tvArticleTitle = (TextView) row
				.findViewById(R.id.tvArticleTitle);
		TextView tvNewspaperName = (TextView) row
				.findViewById(R.id.tvNewspaperName);
		TextView tvNumberOfComment = (TextView) row
				.findViewById(R.id.tvNumberOfComment);
		ImageView imgArticle = (ImageView) row.findViewById(R.id.imgArticle);
		TextView tvICComment = (TextView) row.findViewById(R.id.tvICComment);

		tvArticleTitle.setText(article.getTitle());
		tvNewspaperName.setText(article.getSite());

		if (article.getImage() == null || article.getImage().length() == 0) {
			imgArticle.setVisibility(View.GONE);
		} else {
			imgLoader.displayImage(article.getImage(), imgArticle, options);
			imgArticle.setVisibility(View.VISIBLE);
		}
		tvNumberOfComment.setText(article.getCmtCount());
		if (Integer.valueOf(article.getCmtCount()) < 50) {// black
			tvNumberOfComment.setTextColor(Color.parseColor("#808080"));
			tvICComment.setBackgroundResource(R.drawable.ic_comment_dislike);
		} else {// red
			tvNumberOfComment.setTextColor(Color.parseColor("#f2616f"));
			tvICComment.setBackgroundResource(R.drawable.ic_comment_like);
			tvArticleTitle.setTypeface(null, Typeface.BOLD);
			tvArticleTitle.setTextColor(Color.parseColor("#f2616f"));
			// row.setBackgroundColor(Color.parseColor("#fff0ed"));
		}

		return row;
	}

	/*private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections
				.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}*/

}
